<?php
/**
 * Created by IntelliJ IDEA.
 * User: ty42
 * Date: 4/28/16
 * Time: 6:43 AM
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Notify_post_extendm extends CI_Model
{
    const INTERVAL = 1800;  // 30min
    const GAP_MAX = 36 * 60 * 60;   // 72 * 1800

    const STAT_START = 0;
    const STAT_STOP = 1;

    static $rslt_arr = array(
        "4" => "提问",
        "5" => "投票",
    );

    function __construct()
    {
        parent::__construct();
    }

    public function get_tasks()
    {
        $whe = array("stat" => 0, "notify_timestamp<=" => time());
        $query = $this->db->where($whe)->get('ml_notify_post_extend');
        if (!$query) {
            return null;
        }

        $result = $query->result_array();
        return $result;
    }


    public function insert_notify_task($post_id, $post_type_extend)
    {
        if ($post_type_extend != 4 && $post_type_extend != 5)
        {
            return false;
        }

        $data = array(
            "post_id" => $post_id,
            "post_type_extend" => $post_type_extend,
            "atime" => time(),
        );
        return $this->db->insert('ml_notify_post_extend', $data);
    }

    public function get_num_vote($post_id, $post_extend)
    {
        if ($post_extend != 5) {
            return 0;
        }
        try {
            $num = $this->postm->get_total_count($post_id);
        }
        catch (Exception $e) {
            @w_log("get_num_vote, post_id:" . $post_id . ", " . $e);
            $num = 0;
        }
        return $num;
    }

    public function get_num_comment($post_id, $post_extend)
    {
        $this->db->where("post_id", $post_id);
        $this->db->where("is_show", 1);
        $count = $this->db->count_all_results('ml_comment');
        return $count;
    }

    public function notify_relate_user($post_id, $author_id, $msg)
    {
//        print_r(func_get_args());
        $this->load->model('messagem');

        $this->messagem->send_message($post_id, $author_id, $msg, '31495');
    }

    public function run_tasks()
    {
        $this->load->model('postm');
        $tasks = $this->get_tasks();
        if (empty($tasks)) {
            return true;
        }

        foreach ($tasks as $task) {
            $data_update = array();

            $now = time();
            
            $post = $this->postm->get_post_info($task["post_id"]);

            if (!$post["is_show"]) {
                $data_update["stat"] = self::STAT_STOP;
                $data_update["utime"] = time();
            }
            else {
                // 统计关键数据
                $num_new_cmmnt = $this->get_num_comment($task["post_id"], $task["post_type_extend"]);
                $num_new_vote = $this->get_num_vote($task["post_id"], $task["post_type_extend"]);

                if ($num_new_cmmnt <= $task["num_old_comment"] && $num_new_vote <= $task["num_old_vote"]) {
                    $data_update["turn_empty"] = $task["turn_empty"] + 1;

                    if ($data_update["turn_empty"] >= intval(self::GAP_MAX / self::INTERVAL)) {
                        $data_update["stat"] = self::STAT_STOP;
                    }
                } else {
                    if (!isset(self::$rslt_arr[$task["post_type_extend"]])) {
                        continue;
                    }

                    if ($num_new_vote > $task["num_old_vote"]) {
                        $n_add = $num_new_vote - $task["num_old_vote"];
                        $msg = $n_add . "人刚参与了你发起的投票: " . $post["subject"];
                        $this->notify_relate_user($task["post_id"], $post["author_id"], $msg);
                    }

                    if ($num_new_cmmnt > $task["num_old_comment"]) {
                        $opt = self::$rslt_arr[$task["post_type_extend"]];
                        $n_add = $num_new_cmmnt - $task["num_old_comment"];
                        $msg = $n_add . "人刚评论了你发起的" . $opt . ": " . $post["subject"];
                        $this->notify_relate_user($task["post_id"], $post["author_id"], $msg);
                    }

                    $data_update["turn_empty"] = 0;
                }

                $data_update["notify_timestamp"] = $now + self::INTERVAL;
                $data_update["num_old_comment"] = $num_new_cmmnt;
                $data_update["num_old_vote"] = $num_new_vote;
                $data_update["utime"] = time();
            }

            $this->db->where("pnid", $task["pnid"]);
            $this->db->update('ml_notify_post_extend', $data_update);
        }
        return true;
    }
}